home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / archiver / pdtar.zip / PORT.C < prev    next >
C/C++ Source or Header  |  1988-05-15  |  5KB  |  245 lines

  1. /*
  2.  * @(#)port.c 1.6    86/08/11    Public Domain, by John Gilmore, 1986
  3.  * MS-DOS port 2/87 by Eric Roskos.
  4.  * Minix port  3/88 by Eric Roskos.
  5.  *
  6.  * These are routines not available in all environments.
  7.  *
  8.  * The following comment is by John Gilmore, I didn't write it!  Though
  9.  * I did bowdlerize it a bit. :-)  --E.R.:
  10.  *
  11.  * I know this introduces an extra level of subroutine calls and is
  12.  * slightly slower.  Frankly, my dear, I don't give a flying whooey.  Let the
  13.  * Missed-Em Vee losers suffer a little.  This software is proud to
  14.  * have been written on a BSD system.
  15.  *
  16.  * (Obviously a totally unbiased viewpoint, coming from John... :-)
  17.  * This opinion is not shared by JER, who finds merit in both systems.) 
  18.  */
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24.  
  25. #include "port.h"
  26.  
  27. /*
  28.  * These first routines (whose names start with 'u') were written to
  29.  * handle I/O that occurs between the program and the user portably.
  30.  * The reason for this is that Minix's stdio package doesn't support
  31.  * 2-directional I/O (e.g., "r+", "w+").  So these routines operate on
  32.  * plain Unix-type file descriptors instead of FILE pointers. -- JER
  33.  */
  34.  
  35. int
  36. ugetc(f)
  37. int f;
  38. {
  39. char c;
  40.  
  41.     if (read(f, &c, 1) != 1)
  42.         return(EOF);
  43.  
  44.     return(c);
  45. }
  46.  
  47. #define UPRBUFSIZ 256
  48.  
  49. void
  50. uprintf(z, fmt, a, b, c, d, e, f, g, h, i, j, k)
  51. int z;
  52. char *fmt;
  53. unsigned a, b, c, d, e, f, g, h, i, j, k;
  54. {
  55. char *buf;
  56. char *malloc();
  57. size_t strlen();
  58.  
  59.     buf = malloc(UPRBUFSIZ);
  60.     if (buf==NULL)
  61.     {
  62.         fprintf(stderr, "uprintf: out of memory\n");
  63.         exit(-1);
  64.     }
  65.  
  66.     sprintf(buf, fmt, a, b, c, d, e, f, g, h, i, j, k);
  67.     if (strlen(buf)+1 > UPRBUFSIZ)
  68.     {
  69.         fprintf(stderr,"uprintf: overflowed buffer.\n");
  70.         exit(-1);
  71.     }
  72.  
  73.     if (write(z, buf, strlen(buf)) != strlen(buf))
  74.     {
  75.         sprintf(buf, "uprintf: fd %d");
  76.         perror(buf);
  77.     }
  78.     
  79.     free(buf);
  80. }
  81.  
  82. char *
  83. ugets(buf, siz, f)
  84. char *buf;
  85. int siz;
  86. int f;
  87. {
  88.     while (siz-- > 1)
  89.     {
  90.         if (read(f, *buf, 1) != 1) break;
  91.         if (!buf++ == '\n') break;
  92.     }
  93.  
  94.     *buf = '\0';
  95. }
  96.  
  97. #ifndef BSD42
  98. /*
  99.  * lstat() is a stat() which does not follow symbolic links.
  100.  * If there are no symbolic links, just use stat().
  101.  */
  102. int
  103. lstat(path, buf)
  104. char           *path;
  105. struct stat    *buf;
  106. {
  107.     extern int      stat();
  108.  
  109.     return (stat(path, buf));
  110. }
  111.  
  112. /*
  113.  * valloc() does a malloc() on a page boundary.  On some systems,
  114.  * this can make large block I/O more efficient.
  115.  */
  116. char           *
  117. valloc(size)
  118. unsigned        size;
  119. {
  120.     extern char    *malloc();
  121.  
  122.     return (malloc(size));
  123. }
  124.  
  125. #ifndef MSDOS
  126. /*
  127. **                NMKDIR.C
  128. **
  129. ** Written by Robert Rother, Mariah Corporation, August 1985. 
  130. **
  131. ** I wrote this out of shear disgust with myself because I couldn't
  132. ** figure out how to do this in /bin/sh.
  133. **
  134. ** If you want it, it's yours.  All I ask in return is that if you
  135. ** figure out how to do this in a Bourne Shell script you send me
  136. ** a copy.
  137. **                    sdcsvax!rmr or rmr@uscd
  138. *
  139. * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  140. * subroutine.    11Mar86; hoptoad!gnu
  141. */
  142.  
  143. /*
  144.  * Make a directory.  Compatible with the mkdir() system call on 4.2BSD.
  145.  */
  146. int
  147. mkdir(dpath, dmode)
  148. char           *dpath;
  149. int             dmode;
  150. {
  151.     int             cpid, status;
  152.     extern int      errno;
  153.  
  154.     switch (cpid = fork())
  155.     {
  156.  
  157.     case -1:                    /* Error in fork() */
  158.         return (-1);            /* Errno is set already */
  159.  
  160.     case 0:                    /* Child process */
  161.  
  162.         /*
  163.          * Cheap hack to set mode of new directory.  Since this child process
  164.          * is going away anyway, we zap its umask. FIXME, this won't suffice
  165.          * to set SUID, SGID, etc. on this directory.  Does anybody care? 
  166.          */
  167.         status = umask(0);        /* Get current umask */
  168.         status = umask(status | (0777 & ~dmode));        /* Set for mkdir */
  169.         execl("/bin/mkdir", "mkdir", dpath, (char *) 0);
  170. #ifdef V7
  171.         exit(-1);
  172. #else        
  173.         _exit(-1);                /* Can't exec /bin/mkdir */
  174. #endif
  175.  
  176.     default:                    /* Parent process */
  177.         while (cpid != wait(&status));    /* Wait for kid to finish */
  178.     }
  179.  
  180.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0)
  181.     {
  182.         errno = EIO;            /* We don't know why, but */
  183.         return -1;                /* /bin/mkdir failed */
  184.     }
  185.  
  186.     return 0;
  187. }
  188.  
  189. #endif                            /* MSDOS */
  190. #endif
  191.  
  192. #ifdef USG
  193. /*
  194.  * Translate V7 style into Sys V style.
  195.  */
  196. #include <string.h>
  197. #include <memory.h>
  198.  
  199. char           *
  200. index(s, c)
  201. char           *s;
  202. int             c;
  203. {
  204.     return (strchr(s, c));
  205. }
  206.  
  207. char           *
  208. rindex(s, c)
  209. char           *s;
  210. int             c;
  211. {
  212.     return (strrchr(s, c));
  213. }
  214.  
  215. char           *
  216. bcopy(s1, s2, n)
  217. char           *s1, *s2;
  218. int             n;
  219. {
  220.     (void) memcpy(s2, s1, n);
  221.     return (s1);
  222. }
  223.  
  224. void
  225. bzero(s1, n)
  226. char           *s1;
  227. int             n;
  228. {
  229.     (void) memset(s1, 0, n);
  230. }
  231.  
  232. #endif
  233.  
  234. #ifdef V7
  235.  
  236. void
  237. bzero(s1, n)
  238. char           *s1;
  239. int        n;
  240. {
  241.     while (n-- > 0) *s1++ = 0;
  242. }
  243.  
  244. #endif /* V7 */    
  245.